<?php

/**
 * turma actions.
 *
 * @package    ibe
 * @subpackage turma
 * @author     Creatix
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class turmaActions extends sfActionsCreatix
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->forward('turma', 'list');
	}

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= TurmaTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url'));

		foreach($this->data as $key => &$r)
		{
			
			$actions = array(
				'edit'	 => "<a href='".url_for($this->getModuleName().'/edit?id_turma='.$r['id_turma'])."' class='icoEditar'></a>",
				//'show' => "<a href='".url_for($this->getModuleName().'/show?id_turma='.$r['id_turma'])."'  class='icoVisualizar'></a>",
				//'aluno' => "<a href='".url_for($this->getModuleName().'/alunos?id_turma='.$r['id_turma'])."' class='icoAdicionar' title='Adicinar Aluno'></a>",
				'delete' => "<a href='".url_for($this->getModuleName().'/delete?id_turma='.$r['id_turma'])."'  class='icoExcluir delete'></a>",
			);
			
			$r['act'] 		= implode(' ', $actions);
			//$r['st_turma'] 	= Turma::getStTurma();
		}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}

	public function executeList()
	{
		$this->form = new TurmaForm();
		$this->colunas	= array(
			$this->form['id_turma']->renderLabel() => "id_turma",
			$this->form['nm_turma']->renderLabel() => "nm_turma",
			$this->form['nu_turma']->renderLabel() => "nu_turma",
			$this->form['ds_turma']->renderLabel() => "ds_turma",
			//$this->form['st_turma']->renderLabel() => "st_turma",
			//'Número de alunos' => 'qtde_alunos',
			"Ações"	=> "act"
		);

		$this->filter = $this->_getFilter();
		$this->setTemplate('index');
	}

	public function executeFilter()
	{
		$filter = new Filtro(
			$this->getRequestParameter(sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

	public function executeShow(sfWebRequest $request)
	{
		$this->turma = Doctrine::getTable('Turma')->find(array($request->getParameter('id_turma')));
		$this->forward404Unless($this->turma);
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new TurmaForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));
		$this->form = new TurmaForm();
		$this->processForm($request, $this->form);
		$this->forward($this->getModuleName(), 'new');
	}

	public function validateCreate()
	{
		return true;
	}

	public function handleErrorCreate()
	{
		return $this->forward($this->getModuleName(), 'new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$turma = Doctrine::getTable('Turma')->find($request->getParameter('id_turma'));
		$this->forward404Unless($turma, sprintf('A turma informada (%s) não existe.', $request->getParameter('id_turma')));
		
		$this->form 			= new TurmaForm($turma);
		$this->formAnoLetivo 	= new TurmaAnoLetivoForm();
		$this->turmas 			= TurmaAnoLetivoTable::carregarAnosLetivos($turma->getIdTurma());
		
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$turma = Doctrine::getTable('Turma')->find($request->getParameter('id_turma'));
		$this->forward404Unless($turma, sprintf('A turma informada (%s) não existe.', $request->getParameter('id_turma')));
		$this->form = new TurmaForm($turma);

		$this->processForm($request, $this->form);
		$this->forward($this->getModuleName(), 'edit');
	}

	public function validateUpdate()
	{
		return true;
	}

	public function handleErrorUpdate()
	{
		return $this->forward($this->getModuleName(), 'edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();

			$this->forward404Unless($turma = Doctrine::getTable('Turma')->find(array($request->getParameter('id_turma'))), sprintf('Object turma does not exist (%s).', $request->getParameter('id_turma')));
//			$turma->delete();
			$turma->setStTurma('E');
			$turma->save();
			$this->redirect('turma/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('turma/index');
		}

	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		try
		{
			$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
			if ($form->isValid())
			{
				$turma = $form->save(); // Salva os dados b�sicos relativos à turma
				
				$this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
				$this->redirect('turma/edit?id_turma='.$turma->getIdTurma());
			}
			else
			{
				throw new Exception($form->getErrorSchema());
			}
		}
		catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
	}
	
}
